import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_vant_kit/widgets/addressList.dart';
import 'package:flutter_vant_kit/widgets/dialog.dart';
import 'package:flutter_vant_kit/widgets/price.dart';
import 'package:provider/provider.dart';
import 'package:shoes_shop/api/api.dart';
import 'package:shoes_shop/core/utils.dart';
import 'package:shoes_shop/pages/order/order.dart';
import 'dart:math' as math;
import 'package:shoes_shop/pages/userInfo/addressList/address_list_page.dart';
import 'package:shoes_shop/pages/userInfo/addressList/address_list_provider.dart';

// 订单详情
class OrderDetail extends StatelessWidget {
  final List goodsList;
  final String orderId;
  const OrderDetail({Key key, this.goodsList, this.orderId}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (goodsList.length == 0) {
      getOrderDetail(orderId);
    }
    AddressInfo _address =
        Provider.of<AddressListProvider>(context, listen: true).selectedAddress;
    List<Widget> _goodsListWidget = [];
    double total = 0;
    for (int i = 0; i < goodsList.length; i++) {
      total = total +
          goodsList[i]['count'] * goodsList[i]['shoes_info'][0]['price'];
      _goodsListWidget.add(
        Container(
          padding: EdgeInsets.only(bottom: 20.w),
          child: Row(
            children: [
              Transform.rotate(
                angle: -math.pi / 7,
                child: Image.network(
                  goodsList[i]['shoes_info'][0]['imgs'][0],
                  width: 150.w,
                  height: 150.w,
                ),
              ),
              SizedBox(
                width: 30.w,
              ),
              Expanded(
                child: Container(
                  height: 150.w,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('${goodsList[i]['shoes_info'][0]['title']}'),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Text(
                                '单价：',
                                style: TextStyle(color: Colors.black45),
                              ),
                              Price(
                                size: 30.w,
                                value: goodsList[i]['shoes_info'][0]['price'],
                                color: Colors.black45,
                              ),
                            ],
                          ),
                          Text('x${goodsList[i]['count']}'),
                        ],
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      );
    }
    return Scaffold(
      appBar: AppBar(
        elevation: 0, // 隐藏阴影
        title: Text('确认订单'),
      ),
      body: Column(
        children: [
          InkWell(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => AddressListPage(isSelect: true,),
                ),
              ).then((value) {
                int _addressIndex =
                    Provider.of<AddressListProvider>(context, listen: false)
                        .index;
                List<AddressInfo> _addressList =
                    Provider.of<AddressListProvider>(context, listen: false)
                        .addressList;
                Provider.of<AddressListProvider>(context, listen: false)
                    .changeAddress(_addressList[_addressIndex]);
              });
            },
            child: Container(
              color: Colors.white,
              padding: EdgeInsets.fromLTRB(30.w, 20.w, 0, 20.w),
              child: Row(
                children: [
                  _address == null
                      ? Expanded(
                          child: Text(
                            '您没有设置默认地址，请选择收货地址',
                            style: TextStyle(color: Colors.black45),
                          ),
                        )
                      : Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('收货人：${_address.name}'),
                                  Text('${_address.tel}'),
                                ],
                              ),
                              SizedBox(
                                height: 10.w,
                              ),
                              Text(
                                  '${_address.province}${_address.city}${_address.county}${_address.addressDetail}')
                            ],
                          ),
                        ),
                  Icon(
                    Icons.chevron_right,
                    size: 60,
                    color: Colors.black26,
                  ),
                ],
              ),
            ),
          ),
          AddressLine(),
          Expanded(
            child: Stack(
              children: [
                ListView(
                  physics: BouncingScrollPhysics(),
                  children: [
                    SizedBox(
                      height: 30.w,
                    ),
                    Container(
                      padding: EdgeInsets.all(30.w),
                      color: Colors.white,
                      child: IntrinsicWidth(
                        child: Column(
                          children: [
                            Column(
                              children: _goodsListWidget,
                            ),
                            Container(
                              padding: EdgeInsets.only(top: 20.w),
                              decoration: BoxDecoration(
                                  border: Border(
                                top:
                                    BorderSide(width: 1, color: Colors.black12),
                              )),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  Text('合计：'),
                                  Price(
                                    value: total,
                                    size: 30.w,
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 100.w,
                    ),
                  ],
                ),
                Positioned(
                  left: 0,
                  bottom: 25.w,
                  child: Container(
                    width: 750.w,
                    child: Center(
                      child: InkWell(
                        onTap: () async {
                          if (_address == null) {
                            showToast('请选择收获地址');
                          }
                          Map res = await payment(orderId, _address);
                          if (res['code'] == 200) {
                            showDialog(
                              context: context,
                              builder: (_) {
                                return NDialog(
                                  message: res['message'],
                                  showCancelButton: false,
                                  onConfirm: () {
                                    Navigator.pushReplacement(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) => OrderPage(),
                                        ));
                                    Provider.of<AddressListProvider>(context,
                                            listen: false)
                                        .changeAddress(null);
                                  },
                                );
                              },
                            );
                          } else {
                            showToast(res['message']);
                          }
                        },
                        child: Container(
                          decoration: BoxDecoration(
                            color: Colors.blue,
                            borderRadius: BorderRadius.all(
                              Radius.circular(40.w),
                            ),
                          ),
                          width: 700.w,
                          height: 80.w,
                          child: Center(
                              child: Text(
                            '立即支付',
                            style: TextStyle(color: Colors.white),
                          )),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

// 地址栏线
class AddressLine extends StatelessWidget {
  const AddressLine({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double num = 0;
    List<double> _numList = [];
    List<Color> _colorList = [];
    List<Color> _colorList1 = [
      Color.fromRGBO(235, 149, 143, 1),
      Color.fromRGBO(235, 149, 143, 1),
      Colors.white,
      Colors.white,
      Color.fromRGBO(160, 200, 241, 1),
      Color.fromRGBO(160, 200, 241, 1),
      Colors.white,
      Colors.white,
    ];
    int flag = 0;
    bool isWhite = false;
    for (int index = 0; num < 100; index++) {
      flag++;
      if (flag > 1) {
        flag = 0;
        if (isWhite) {
          num = num + 3;
        } else {
          num = num + 6;
        }
        _numList.add(num / 100);
        isWhite = !isWhite;
      } else {
        _numList.add(num / 100);
      }
    }
    int flag1 = 0;
    for (int j = 0; j < _numList.length; j++) {
      if (j > _colorList1.length - 1) {
        flag1 = (j % _colorList1.length).toInt();
      } else {
        flag1 = j;
      }
      _colorList.add(_colorList1[flag1]);
    }
    return Container(
      height: 15.w,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          //渐变位置
          transform: GradientRotation(math.pi / 5),
          begin: Alignment(-0.85, 0),
          end: Alignment(0.85, 0),
          stops: _numList,
          colors: _colorList,
        ),
      ),
    );
  }
}
